/*
 *    Copyright 2009-2023 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.ryan.dao;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.ryan.builder.Builder;
import org.apache.ibatis.ryan.builder.CountBuilder;
import org.apache.ibatis.ryan.builder.DeleteBuilder;
import org.apache.ibatis.ryan.builder.InsertBuilder;
import org.apache.ibatis.ryan.builder.NativeSQLBuilder;
import org.apache.ibatis.ryan.builder.QueryBuilder;
import org.apache.ibatis.ryan.builder.UpdateBuilder;
import org.apache.ibatis.ryan.domain.PagedList;
import org.apache.ibatis.ryan.mapper.BaseAutoMapper;
import org.apache.ibatis.ryan.utils.PageHoder;

public class AutoDaoImpl implements AutoBaseDao {
  private BaseAutoMapper autoMapper;

  public <T> int save(T pojo) {
    InsertBuilder<T> builder = InsertBuilder.build(pojo).optSql();
    return this.autoMapper.insert(builder.getSql(), builder.getParams());
  }

  public int save(InsertBuilder<?> builder) {
    return this.autoMapper.insert(builder.getSql(), builder.getParams());
  }

  public int deleteById(String table, Object id) {
    DeleteBuilder<?> builder = DeleteBuilder.build(table).whereEq("id", id);
    return this.autoMapper.delete(builder.getSql(), builder.getParams());
  }

  public int deleteByColum(String table, String propName, Object value) {
    DeleteBuilder<?> builder = DeleteBuilder.build(table).whereEq(propName, value);
    return this.autoMapper.delete(builder.getSql(), builder.getParams());
  }

  public int delete(DeleteBuilder<?> builder) {
    return this.autoMapper.delete(builder.getSql(), builder.getParams());
  }

  public int update(UpdateBuilder builder) {
    return this.autoMapper.update(builder.getSql(), builder.getParams());
  }

  public <T> int updateByColName(T beanSet, String propName, Object value) {
    UpdateBuilder builder = UpdateBuilder.build(beanSet.getClass()).whereEq(propName, value);
    builder.setBean(beanSet);
    return this.autoMapper.update(builder.getSql(), builder.getParams());
  }

  public <T> List<T> listByColName(Class<T> resultType, String propName, Object value) {
    QueryBuilder<?> query = QueryBuilder.build(resultType).whereEq(propName, value);
    List<T> list = this.autoMapper.selectAll(resultType, query.getSql(), query.getParams());
    return list;
  }

  public <T> PagedList<T> listPageByColName(Class<T> resultType, String propName, Object value) {
    this.checkPage();
    QueryBuilder<T> query = QueryBuilder.build(resultType).whereEq(propName, value);
    PagedList<T> pagedList = this.autoMapper.selectPage(resultType, query.getSql(), query.getParams());
    return pagedList;
  }

  public <T> PagedList<T> listPage(Builder<T> query) {
    this.checkPage();
    PagedList<T> pagedList = autoMapper.selectPage(query.getResultType(), query.getSql(), query.getParams());
    return pagedList;
  }

  /** @deprecated */
  @Deprecated
  public <T> PagedList<T> listPage(QueryBuilder<T> query) {
    this.checkPage();
    PagedList<T> pagedList = autoMapper.selectPage(query.getResultType(), query.getSql(), query.getParams());
    return pagedList;
  }

  public <T> List<T> listAll(Builder<T> query) {
    List<T> all = this.autoMapper.selectAll(query.getResultType(), query.getSql(), query.getParams());
    return all;
  }

  /** @deprecated */
  @Deprecated
  public <T> List<T> listAll(QueryBuilder<T> query) {
    List<T> all = this.autoMapper.selectAll(query.getResultType(), query.getSql(), query.getParams());
    return all;
  }

  public <T> T findOne(Builder<T> query) {
    T o = this.autoMapper.selectOne(query.getResultType(), query.getSql(), query.getParams());
    return o;
  }

  public <T> T findOneByColName(Class<T> rsType, String propName, Object propValue) {
    QueryBuilder<T> builder = QueryBuilder.build(rsType).whereEq(propName, propValue);
    List<T> ts = autoMapper.selectAll(builder.getResultType(), builder.getSql(), builder.getParams());
    return ts != null && !ts.isEmpty() ? ts.get(0) : null;
  }

  public <T> T findById(Class<T> rsType, Object id) {
    QueryBuilder<T> query = QueryBuilder.build(rsType).whereEq("id", id);
    T o = this.autoMapper.selectOne(rsType, query.getSql(), query.getParams());
    return o;
  }

  public void setAutoMapper(BaseAutoMapper autoMapper) {
    this.autoMapper = autoMapper;
  }

  /**
   * 检查分页参数是否设置正确
   */
  private void checkPage() {
    if (PageHoder.getPageNo() == null) {
      throw new IllegalArgumentException("PageNo\u53c2\u6570\u5fc5\u4f20!");
    } else if (PageHoder.getPageSize() == null) {
      throw new IllegalArgumentException("PageSize\u53c2\u6570\u5fc5\u4f20!");
    }
  }

  public BigDecimal count(CountBuilder query) {
    // BigDecimal count = (BigDecimal) this.autoMapper.selectOne(BigDecimal.class,
    // query.getSql(), query.getParams());
    // return count;
    return new BigDecimal(0);
  }

  public Integer nativeExecute(NativeSQLBuilder builder) {
    // Integer row = this.autoMapper.nativeExecute(builder.buildSql(),
    // builder.getParams());
    // return row;
    return 0;
  }
}
